#include "ZigbeeListXml.h"

ZigbeeListXml::ZigbeeListXml()
{
}

bool ZigbeeListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "Zigbee")   //解析Device
        {
            QDomNode zigbeeChild = rootChild.toElement().firstChild();
            ZigbeeStruct zigbee;
            while(!zigbeeChild.isNull())
            {
                if (zigbeeChild.toElement().tagName() == "MAC")         //获取Zigbee的MAC地址
                {
                    QString mac = zigbeeChild.toElement().text().remove(" ");
                    zigbee.setMac(QByteArray::fromHex(mac.toLatin1()));

                }
                else if (zigbeeChild.toElement().tagName() == "NWK")    //获取Zigbee的NWK地址
                {
                    QString nwk = zigbeeChild.toElement().text().remove(" ");
                    zigbee.setNwk(QByteArray::fromHex(nwk.toLatin1()));

                }
                else if (zigbeeChild.toElement().tagName() == "AddressList") //获取Zigbee的点位地址列表
                {
                    QStringList addressList = zigbeeChild.toElement().text().split(",");
                    QList<ControlPointStruct> pointList;
                    foreach (const QString &address, addressList)
                    {
                        ControlPointStruct point;
                        point.setAddress(address.toUInt());
                        pointList.append(point);
                    }
                    zigbee.setPointList(pointList);
                }

                zigbeeChild = zigbeeChild.nextSibling();
            }
            m_zigbeeList.append(zigbee);
        }
        rootChild = rootChild.nextSibling();
    }
    return true;
}

bool ZigbeeListXml::modify(const quint8 &number, const ZigbeeStruct &zigbee)
{

    if(!this->check())
    {
        return false;
    }

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();
    QDomNode rootChild = root.firstChild();

    ZigbeeStruct completeZigbee = zigbee;
    for(quint16 index=0; index<m_zigbeeList.size(); index++)
    {
        ZigbeeStruct tempZigbee = m_zigbeeList.at(index);
        if(tempZigbee.mac() == completeZigbee.mac())
        {
            if(number != 0)
            {
                QList<ControlPointStruct> pointList = tempZigbee.pointList();
                pointList.append(completeZigbee.pointList());
                completeZigbee.setPointList(pointList);
            }
        //    return false;
            m_zigbeeList.removeAt(index);
            break;
        }
    }

    m_zigbeeList.append(completeZigbee);


    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "Zigbee")   //解析Device
        {
            QDomNodeList nodeList = rootChild.childNodes();
            for(int i=0; i<nodeList.size(); i++)
            {
                QDomNode node = nodeList.at(i);
                if(node.toElement().tagName() == "MAC")
                {
                    QByteArray mac = QByteArray::fromHex(node.toElement().text().remove(" ").toLatin1());
                    if(mac == completeZigbee.mac())
                    {
                 //       return false;
                        root.removeChild(rootChild);
                    }
                }
            }
        }
        rootChild = rootChild.nextSibling();
    }


    QDomElement zigbeeElement = doc.createElement("Zigbee");
    QDomText text;
    QDomElement macElement = doc.createElement("MAC");
    QString mac = completeZigbee.mac().toHex().toUpper();
    int length = mac.length()/2;
    for(int i=1; i<length; i++)
    {
        mac.insert(2*i+i-1," ");
    }
    text = doc.createTextNode(mac);
    macElement.appendChild(text);
    zigbeeElement.appendChild(macElement);

    QDomElement nwkElement = doc.createElement("NWK");
    QString nwk = completeZigbee.nwk().toHex().toUpper();
    length = nwk.length()/2;
    for(int i=1; i<length; i++)
    {
        nwk.insert(2*i+i-1," ");
    }
    text = doc.createTextNode(nwk);
    nwkElement.appendChild(text);
    zigbeeElement.appendChild(nwkElement);


    QDomElement addressListElement = doc.createElement("AddressList");

    QStringList tempAddressList;
    for(int i=0; i<completeZigbee.pointList().size(); i++)
    {
        tempAddressList.append(QString::number(completeZigbee.pointList().at(i).address()));
    }
    QString addressList = tempAddressList.join(",");

    text = doc.createTextNode(addressList);
    addressListElement.appendChild(text);
    zigbeeElement.appendChild(addressListElement);

    root.appendChild(zigbeeElement);

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    return true;
}

bool ZigbeeListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_zigbeeList.clear();
    return true;
}

QList<ZigbeeStruct> ZigbeeListXml::zigbeeList(const QList<ControlPointStruct> &sourcePointList)
{
    QList<ZigbeeStruct> zigbeeList;
    foreach (const ZigbeeStruct &zigbee, m_zigbeeList)
    {
        ZigbeeStruct tempZigbee;
        tempZigbee.setMac(zigbee.mac());
        tempZigbee.setNwk(zigbee.nwk());
        QList<ControlPointStruct> controlPointList;
        foreach (const ControlPointStruct &point, zigbee.pointList())
        {
            foreach(const ControlPointStruct &sourcePoint, sourcePointList)
            {
                if(point.address() == sourcePoint.address())
                {
                    controlPointList.append(sourcePoint);
                }
            }
        }

        if(!controlPointList.isEmpty())
        {
            int size = controlPointList.size();
            int count = (size + PointMaxSize - 1) / PointMaxSize;

            for (int i=0; i<count; i++)
            {
                QList<ControlPointStruct> tempPointList = controlPointList.mid(i*PointMaxSize, ((i+1)*PointMaxSize>size ? size:PointMaxSize*(i+1)));
                tempZigbee.setPointList(tempPointList);
                zigbeeList.append(tempZigbee);
            }
        }
    }
    return zigbeeList;
}
